package com.paobuji.aidebao.myapplication.view;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateInterpolator;

import com.paobuji.aidebao.myapplication.R;
import com.paobuji.aidebao.myapplication.tool.PxUtils;

/**
 * Created by lenovo on 2017/3/20.
 */

public class TargerSeekBar extends View {
    private Context mContext ;

    //画笔类
    private Paint PaintOutCircleback ;
    private Paint PaintInCircleback ;
    private Paint PaintOutCircle ;
    private Paint PaintInCircle ;
    private Paint PaintCurrentKM ;
    private Paint PaintMark ;
    private Paint PaintCuttentDay ;
    private Paint PaintLittleMark ;

    //动画效果
    ValueAnimator valueAnimator1 = null ;
    ValueAnimator valueAnimator2 = null ;


    private int mWidth, mHight;

    private int wOutCricleRadius ;
    private int wInCricleRadius ;
    private float OutStrokeWidth ;

    private int size1 ,size2 ,size3 ,size4 ;

    private int OutMax = 3000 ; // 外圆的最大值,即目标距离 ,默认为3000
    private int InMax = 30  ; //内圆的最大值,即天数,默认为30
    private int currtKm ; //当前距离
    private int currtDay ; //当前为第几天

    public int getOutMax() {
        return OutMax;
    }

    public void setOutMax(int outMax) {
        OutMax = outMax;
    }

    public int getInMax() {
        return InMax;
    }

    public void setInMax(int inMax) {
        InMax = inMax;
    }


    public int getCurrtDay() {
        return currtDay;
    }

    public int getCurrtKm() {
        return currtKm;
    }

    public void setKmAndDay(int currtKm , int currtDay){
        /*this.currtDay = currtDay ;
        this.currtKm = currtKm;
        invalidate();*/
        setAnimator(currtKm , currtDay);
    }

    private RectF rectF1 ,rectF2 ;

    public TargerSeekBar(Context context) {
        this(context , null);
    }

    public TargerSeekBar(Context context, AttributeSet attrs) {
        this(context, attrs , 0);
    }

    public TargerSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context ;
        //初始化属性
        initAttr(attrs);
    }

    public void initAttr(AttributeSet attrs){
        //初始化画笔
        /**
         paintProgressBackground = new Paint();
         paintProgressBackground.setAntiAlias(true);
         paintProgressBackground.setStrokeWidth(progressStrokeWidth);
         paintProgressBackground.setStyle(Paint.Style.STROKE);
         paintProgressBackground.setStrokeCap(Paint.Cap.ROUND);
         paintProgressBackground.setColor(dashboardViewattr.getBackground());
         paintProgressBackground.setDither(true);
         */
        initPoint();
        TypedArray ta = mContext.obtainStyledAttributes(attrs, R.styleable.TargerSeekBar);

        PaintOutCircleback.setAntiAlias(true);
        OutStrokeWidth = ta.getDimension(R.styleable.TargerSeekBar_outCircleStrokeWidth ,10 );
        PaintOutCircleback.setStrokeWidth(ta.getDimension(R.styleable.TargerSeekBar_outCircleStrokeWidth ,10 ));
        PaintOutCircleback.setStyle(Paint.Style.STROKE);
        PaintOutCircleback.setStrokeCap(Paint.Cap.ROUND);
        PaintOutCircleback.setColor(ta.getColor(R.styleable.TargerSeekBar_outCirclebackgroundColor ,mContext.getResources().getColor(R.color.colorAccent) ));
        PaintOutCircleback.setDither(true);

        PaintInCircleback.setAntiAlias(true);
        PaintInCircleback.setStrokeWidth(ta.getDimension(R.styleable.TargerSeekBar_inCircleStrokeWidth ,10 ));
        PaintInCircleback.setStyle(Paint.Style.STROKE);
        PaintInCircleback.setStrokeCap(Paint.Cap.ROUND);
        PaintInCircleback.setColor(ta.getColor(R.styleable.TargerSeekBar_inCirclebackgroundColor ,mContext.getResources().getColor(R.color.colorAccent) ));
        PaintInCircleback.setDither(true);

        PaintOutCircle.setAntiAlias(true);
        PaintOutCircle.setStrokeWidth(ta.getDimension(R.styleable.TargerSeekBar_outCircleStrokeWidth ,10 ));
        PaintOutCircle.setStyle(Paint.Style.STROKE);
        PaintOutCircle.setStrokeCap(Paint.Cap.ROUND);
        PaintOutCircle.setColor(ta.getColor(R.styleable.TargerSeekBar_outCircleColor ,mContext.getResources().getColor(R.color.colorAccent) ));
        PaintOutCircle.setDither(true);

        PaintInCircle.setAntiAlias(true);
        PaintInCircle.setStrokeWidth(ta.getDimension(R.styleable.TargerSeekBar_inCircleStrokeWidth ,10 ));
        PaintInCircle.setStyle(Paint.Style.STROKE);
        PaintInCircle.setStrokeCap(Paint.Cap.ROUND);
        PaintInCircle.setColor(ta.getColor(R.styleable.TargerSeekBar_inCircleColor ,mContext.getResources().getColor(R.color.colorAccent) ));
        PaintInCircle.setDither(true);

        PaintCurrentKM.setAntiAlias(true);
        PaintCurrentKM.setStyle(Paint.Style.FILL);
        PaintCurrentKM.setTextAlign(Paint.Align.LEFT);
        PaintCurrentKM.setColor(ta.getColor(R.styleable.TargerSeekBar_currentKMTextColor ,mContext.getResources().getColor(R.color.colorAccent)) );
        PaintCurrentKM.setTextSize(ta.getDimension(R.styleable.TargerSeekBar_currentKMTextSize , 30));
        size1 = (int) ta.getDimension(R.styleable.TargerSeekBar_currentKMTextSize , 30);

        PaintMark.setAntiAlias(true);
        PaintMark.setStyle(Paint.Style.FILL);
        PaintMark.setTextAlign(Paint.Align.LEFT);
        PaintMark.setColor(ta.getColor(R.styleable.TargerSeekBar_markTextColor ,mContext.getResources().getColor(R.color.colorAccent)) );
        PaintMark.setTextSize(ta.getDimension(R.styleable.TargerSeekBar_markTextSize , 30));
        PaintMark.setFakeBoldText(true);
        size2 = (int) ta.getDimension(R.styleable.TargerSeekBar_markTextSize , 30);

        PaintCuttentDay.setAntiAlias(true);
        PaintCuttentDay.setStyle(Paint.Style.FILL);
        PaintCuttentDay.setTextAlign(Paint.Align.LEFT);
        PaintCuttentDay.setColor(ta.getColor(R.styleable.TargerSeekBar_cuttentDayTextColor ,mContext.getResources().getColor(R.color.colorAccent)) );
        PaintCuttentDay.setTextSize(ta.getDimension(R.styleable.TargerSeekBar_cuttentDayTextSize , 30));
        size3 = (int) ta.getDimension(R.styleable.TargerSeekBar_cuttentDayTextSize , 30);

        PaintLittleMark.setAntiAlias(true);
        PaintLittleMark.setStyle(Paint.Style.FILL);
        PaintLittleMark.setTextAlign(Paint.Align.LEFT);
        PaintLittleMark.setColor(ta.getColor(R.styleable.TargerSeekBar_littleMarkTextColor ,mContext.getResources().getColor(R.color.colorAccent)) );
        PaintLittleMark.setTextSize(ta.getDimension(R.styleable.TargerSeekBar_littleMarkTextSize , 30));
        size4 = (int) ta.getDimension(R.styleable.TargerSeekBar_littleMarkTextSize , 30);

        ta.recycle();

    }

    public void initPoint(){
        PaintOutCircleback = new Paint();
        PaintInCircleback = new Paint();
        PaintOutCircle = new Paint();
        PaintInCircle = new Paint();
        PaintCurrentKM = new Paint();
        PaintMark = new Paint();
        PaintCuttentDay = new Paint();
        PaintLittleMark = new Paint();
    }

    //计算控件的高宽
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int realWidth = startMeasure(widthMeasureSpec);
        int realHeight = startMeasure(heightMeasureSpec);

        setMeasuredDimension(realWidth, realHeight);
    }

    //若给的是wrap_context 则为200dp
    private int startMeasure(int msSpec) {
        int result = 0;
        int mode = MeasureSpec.getMode(msSpec);
        int size = MeasureSpec.getSize(msSpec);
        if (mode == MeasureSpec.EXACTLY) {
            result = size;
        } else {
            result = PxUtils.dpToPx(200, mContext);
        }
        return result;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = getWidth();
        mHight = getHeight();
        wOutCricleRadius = (mWidth > mHight ? mHight : mWidth) / 2; //最大圆的半径
        wOutCricleRadius = (int) (wOutCricleRadius - OutStrokeWidth);
        wInCricleRadius  = wOutCricleRadius * 3 /4 ;

        rectF1 = new RectF(-wOutCricleRadius , -wOutCricleRadius , wOutCricleRadius , wOutCricleRadius);

        rectF2 = new RectF(-wInCricleRadius , -wInCricleRadius , wInCricleRadius ,wInCricleRadius);

    };

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.translate(mWidth / 2, mHight / 2);
        //绘制两圆环
        canvas.drawArc(rectF1 , 135 , 270 , false , PaintOutCircleback);
        canvas.drawArc(rectF2 , 135 , 270 , false ,PaintInCircleback);

        drawProess(canvas , currtKm , currtDay);

        //绘制文字
        drawXianShiText(canvas , OutMax , InMax , currtKm ,currtDay);

    }

    public void drawXianShiText(Canvas canvas , int OutMax , int InMax ,int currtKm , int currtDay){
        int tWidth1 = (int) PaintCurrentKM.measureText(currtKm+"");
        int tWidth2 = (int) PaintMark.measureText(getResources().getString(R.string.goalt) + " " +  OutMax + " km");
        int tWidth3 = (int)PaintCuttentDay.measureText(getResources().getString(R.string.day)+currtDay);
        int tWidth4 = (int) PaintLittleMark.measureText(InMax + "");

        canvas.drawText(currtKm+"" , -tWidth1/2 , -wInCricleRadius / 2 , PaintCurrentKM);
        canvas.drawText(getResources().getString(R.string.goalt) + " " + OutMax + " km" , -tWidth2 / 2 ,0, PaintMark);
        canvas.drawText(getResources().getString(R.string.day)+currtDay , -tWidth3 / 2 ,wInCricleRadius / 2, PaintCuttentDay);

        canvas.drawText("0" , (int)(-Math.sqrt(2)*wInCricleRadius/2) * 3/4 ,(int)(Math.sqrt(2)*wInCricleRadius/2 + size4) , PaintLittleMark);
        canvas.drawText(InMax + "" , (int)(Math.sqrt(2)*wInCricleRadius/2 * 3/4- tWidth4 ),(int)(Math.sqrt(2)*wInCricleRadius/2 + size4) , PaintLittleMark);
    }

    public void drawProess(Canvas canvas , int currtKm , int currtDay){
        canvas.drawArc(rectF1 , 135 , currtKm * 270/ OutMax  , false , PaintOutCircle);
        canvas.drawArc(rectF2 , 135 , currtDay * 270 / InMax  , false , PaintInCircle);
    }

    public void setAnimator(int currtKm , int currtDay){
        if(valueAnimator1 != null && valueAnimator1.isRunning()){
            valueAnimator1.cancel();
        }
        if(valueAnimator2 != null && valueAnimator2.isRunning()){
            valueAnimator2.cancel();
        }

        long animaorDuration = 800 ; //动画持续时间为 800ms
        float per1 = (float) (currtKm /OutMax) > 1f ? 1f : (float)currtKm/OutMax ;
        Log.d("差值" , per1  +"");
        valueAnimator1 = ValueAnimator.ofFloat( 0f , per1).setDuration(animaorDuration);
        valueAnimator1.setInterpolator(new AccelerateInterpolator());
        valueAnimator1.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                Log.d("差值" , (float)valueAnimator.getAnimatedValue()  +"");
                TargerSeekBar.this.currtKm = (int) (((float)valueAnimator.getAnimatedValue())* TargerSeekBar.this.OutMax);
                invalidate();
            }
        });
        valueAnimator1.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
            }
        });
        valueAnimator1.start();


        float per2 = (float) (currtDay /InMax) > 1f ? 1f : (float)currtDay /InMax ;
        Log.d("差值" , per2  +"");
        valueAnimator2 = ValueAnimator.ofFloat( 0f , per2).setDuration(animaorDuration);
        valueAnimator2.setInterpolator(new AccelerateInterpolator());
        valueAnimator2.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                Log.d("差值" , (float)valueAnimator.getAnimatedValue()  +"");
                TargerSeekBar.this.currtDay = (int) (((float)valueAnimator.getAnimatedValue())* TargerSeekBar.this.InMax);
                invalidate();
            }
        });
        valueAnimator2.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
            }
        });

        valueAnimator2.start();
    }
}
